﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dzzzr
{
	/// <summary>
	/// Editor for EntityAttribute
	/// </summary>
	public class EntityAttributeCollectionEditor : System.ComponentModel.Design.CollectionEditor
	{
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="type"></param>
		public EntityAttributeCollectionEditor(Type type)
			: base(type)
		{
		}

		/// <summary>
		/// Create new object
		/// </summary>
		/// <param name="itemType"></param>
		/// <returns></returns>
		protected override object CreateInstance(Type itemType)
		{
			EntityAttribute ea = (EntityAttribute)base.CreateInstance(itemType);
			if (this.Context.Instance is Entity)
			{
				ea.Entity = this.Context.Instance as Entity;

				DBDataContext.DataContext.EntityAttributes.InsertOnSubmit(ea);
			}
			return ea;
		}

		/// <summary>
		/// Delete object
		/// </summary>
		/// <param name="instance"></param>
		protected override void DestroyInstance(object instance)
		{
			base.DestroyInstance(instance);
			DBDataContext.DataContext.EntityAttributes.DeleteOnSubmit(instance as EntityAttribute);
		}
	}
}
